/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Implementation.SourceCode.Utilities;

import java.awt.Component;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author apple
 */
public class Utilities {

    public static void setTableProperties(JTable tblVietTat, int rowHeight, int[] colWidth, boolean isCustom) {
        tblVietTat.setRowHeight(rowHeight);
        for(int i = 0 ; i < colWidth.length ; i++){
            tblVietTat.getColumnModel().getColumn(i).setPreferredWidth(colWidth[i]);
            //if(isCustom){
                //tblVietTat.getColumnModel().getColumn(i).setCellRenderer(new Utilities.CustomTableCellRenderer());
            //}
        }
    }

    public static class CustomTableCellRenderer extends JLabel implements TableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
            setFont(new java.awt.Font("Tahoma", 0, 30));
            setText(value.toString()+"");
            return this;
        }
    }
    
    public static String commaSoTien(String soTien){
        String retVal = "";
        String soTienTemp = soTien;
        try{
            if(soTien.length()>3){
                String temp = "";
                while(soTien.length()>3){
                    for(int i = 0 ; i < soTien.length() ; i++){
                        if(i==0)continue;
                        if(i%3==0){
                            temp += ","+soTien.substring(soTien.length()-i,soTien.length());
                            soTien = soTien.substring(0,soTien.length()-i);
                            break;
                        }
                    }
                }
                int sodu = 1;
                if(soTien.length()>5){
                    sodu = soTien.length()%3;
                    if(sodu==0){
                        sodu=3;
                    }
                }else if(soTienTemp.length()==6){
                        sodu=3;
                }else{
                    sodu = soTien.length()%3;
                }
                
                temp = soTien.substring(0,sodu)+temp;
                retVal = temp;
            }else{
                soTien = soTien.replace(",", "") ;
                retVal = soTien;
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return retVal;
    }

    public static void centerForm(Component frm) {
        double hei = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        double wid = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int XPosition = (int) (wid / 2 - frm.getWidth() / 2);
        int YPosition = (int) (hei / 2 - frm.getHeight() / 2);
        frm.setLocation(XPosition, YPosition);
    }

    public static String replaceInsertData(String str) throws Exception{
        String retVal = "";
        try{
            retVal = str.replace("'", "&#39;");
        }catch(Exception ex){
            throw ex;
        }
        return retVal;
    }

    public static String replaceLoadData(String str) throws Exception{
        String retVal = "";
        try{
            retVal = str.replace("&#39;", "'");
        }catch(Exception ex){
            throw ex;
        }
        return retVal;
    }
}
